package com.example.stock.controller;

import com.example.stock.controller.dto.YearPctDTO;
import com.example.stock.excel.MinYearPct;
import com.example.stock.excel.WuguHistory;
import com.example.stock.excel.YearPctStat;
import com.example.stock.model.DrawDownYearSat;
import com.example.stock.service.DailyDataService;
import com.example.stock.service.IndustryDataService;
import com.example.stock.service.WuguService;
import com.example.stock.service.YearDataService;
import com.example.stock.util.DataUtil;
import com.example.stock.util.Result;
import com.example.stock.util.ResultGenerator;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Log4j2
@Slf4j
@CrossOrigin(origins = "*")
@RequestMapping("year")
@RestController
public class YearDataController {

    @Autowired
    WuguService wuguService;

    @Autowired
    YearDataService yearDataService;

    @Autowired
    IndustryDataService industryDataService;

    @Autowired
    DailyDataService dailyDataService;


    @GetMapping("pct/{stock}")
    public Result getYearPctStatByStock(@PathVariable String stock) {

        List<MinYearPct> minYearPcts = yearDataService.getMinYearPctMapGroupByStock().get(stock);
        Map<String, DrawDownYearSat> drawDownYearSatByStock = dailyDataService.getDrawDownYearSatByStock(stock);

        WuguHistory wuguHistory = wuguService.getWuguHistory(stock);

        List<YearPctDTO> ret = new ArrayList<>();
        minYearPcts.forEach(item -> {
            YearPctDTO yearPctDTO = new YearPctDTO();
            BeanUtils.copyProperties(item, yearPctDTO);

            DrawDownYearSat drawDownYearSat = drawDownYearSatByStock.get(item.getYear());
            if (drawDownYearSat != null) {
                yearPctDTO.setMinDrawDown(drawDownYearSat.getDrawdown());
                yearPctDTO.setMinDrawDownDate(drawDownYearSat.getDate());
            }

            if (wuguHistory != null) {
                switch (item.getYear()) {
                    case "2025" -> yearPctDTO.setWugu(wuguHistory.getRankNow());
                    case "2024" -> yearPctDTO.setWugu(wuguHistory.getRank2024());
                    case "2023" -> yearPctDTO.setWugu(wuguHistory.getRank2023());
                    case "2022" -> yearPctDTO.setWugu(wuguHistory.getRank2022());
                    case "2021" -> yearPctDTO.setWugu(wuguHistory.getRank2021());
                    case "2020" -> yearPctDTO.setWugu(wuguHistory.getRank2020());
                    case "2019" -> yearPctDTO.setWugu(wuguHistory.getRank2019());
                    case "2018" -> yearPctDTO.setWugu(wuguHistory.getRank2018());
                    case "2017" -> yearPctDTO.setWugu(wuguHistory.getRank2017());
                    case "2016" -> yearPctDTO.setWugu(wuguHistory.getRank2016());
                }
            }

            ret.add(yearPctDTO);
        });

        return ResultGenerator.genSuccessResult(ret);
    }

    @GetMapping("multiStock")
    public Result multiStock(@RequestParam("stocks") String stocks) {
        String[] split = stocks.split(DataUtil.STOCK_SPLIT);
        List<YearPctStat> ret = new ArrayList<>();

        Arrays.stream(split).forEach(stock -> {
            ret.add(yearDataService.getYearPctStatMap().get(stock));
        });

        return ResultGenerator.genSuccessResult(ret);
    }

    @GetMapping("industry/{industry}")
    public Result industry(@PathVariable String industry) {
        List<String> stocksByTag = industryDataService.getStocksByTag(industry);
        List<YearPctStat> ret = new ArrayList<>();

        stocksByTag.forEach(stock -> {
            ret.add(yearDataService.getYearPctStatMap().get(stock));
        });

        return ResultGenerator.genSuccessResult(ret);
    }
}